<?
function FormatToUnix($TimeString)
{
	
}
#-----------------------------------------------------------------------------------
# Display a progress bar
function ProgressBar($Width, $Total, $Value, $Color = NULL, $Height = NULL, $OuterColor = NULL)
{
	if ($Value == 0)
	{
		$Percentage = 100;
		$Widget_Percentage == $Total;
	}
	else
	{
		$Percentage = round($Value / $Total, 5);
		$Widget_Percentage = $Width * $Percentage;
		$Percentage = $Percentage * 100;
	}
	
	if ($Color != NULL)
		$bg = 'background-color:'. $Color .' !important;background-image:none;';
	
	if ($OuterColor != NULL)
		$bg2 = 'background-color:'. $Color .' !important;background-image:none;';
	
	if ($Height != NULL)
		$sz = 'height:'. $Height .'px !important;';
	
	$String .= round($Percentage) .'% ';
	$String = '<div class="ProgressOutter" style="width:'. $Width .'px;'. $sz . $bg2 .'">';
	$String .= '<div class="ProgressInner" style="width:'. $Widget_Percentage .'px;'. $bg . $sz .'">';
	$String .= '</div></div>';
	
	return $String;
}

# Get title
function GetTitle($DB)
{
	$url = DetectQuery();
	$Page = $url[1];
	
	if (is_numeric($Page) && (strlen($Page) == 7 || strlen($Page) == 8))
	{
		$Character = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_NAME .','. TBL_CHARACTERS_SERVER, false, TBL_CHARACTERS_CICUID ."='". $Page ."'");
		if ($Character[0])
			return $Character[3][TBL_CHARACTERS_NAME] .'\'s ('. $Character[3][TBL_CHARACTERS_SERVER] .') XIVPad';
		else
			return XIVPAD_TITLE .' - Character not found!';
	}
	else
	{
	
		switch($Page)
		{
			default: return XIVPAD_TITLE;
			
			case "-Characters": return XIVPAD_TITLE .' - '. MENU_CHARACTERS;
			case "-Linkshells": return XIVPAD_TITLE .' - '. MENU_LINKSHELLS;
			case "-Mugshots": 	return XIVPAD_TITLE .' - '. MENU_MUGSHOTS;
			case "-Tracker": 	return XIVPAD_TITLE .' - '. MENU_TRACKER;
			case "-Census": 	return XIVPAD_TITLE .' - '. MENU_CENSUS;
			case "-Ranking": 	return XIVPAD_TITLE .' - '. MENU_RANKING;
			case "-Guides": 	return XIVPAD_TITLE .' - '. MENU_GUIDES;
			case "-Misc": 		return XIVPAD_TITLE .' - '. MENU_MISC;
			case "-PM": 		return XIVPAD_TITLE .' - '. HEADER_MESSAGES;
			case "-Contact": 	return XIVPAD_TITLE .' - '. HEADER_CONTACT;
			
			case "-Manager": 	return XIVPAD_TITLE .' - '. HEADER_MANAGER;
		}
	}	
}
#-----------------------------------------------------------------------------------
# XIV Functions
# Returns Caps
function GetCap($type)
{
	$Total_Camps 				= 87;
	$Total_Quests 				= 130;
	$Total_Quests_Unobtainable 	= 8;
	$Total_Factions				= 42;
	$Total_Notorious_Monsters 	= 32;
	$Total_Company_Quests 		= 18;
	
	switch ($type)
	{
		case "camps": return $Total_Camps;	
		case "quests": return $Total_Quests;
		case "quests_unobtainable": return $Total_Quests_Unobtainable;
		case "factions": return $Total_Factions;
		case "nms": return $Total_Notorious_Monsters;
		case "company": return $Total_Company_Quests;
	}
	
}

# Returns classes
function GetClassList()
{
	$classes = array("Pugilist", "Gladiator", "Marauder", "Archer", "Lancer", "Sentinal", 									//	War
					 "Thaumaturge", "Conjurer", 																			//	Magic
					 "Carpenter", "Blacksmith", "Armorer", "Goldsmith", "Tanner", "Weaver", "Alchemist", "Culinarian",		//	Hand
					 "Miner", "Botanist", "Fisher");																		//	Land
					 
	return $classes;
}

# Returns servers
function GetServerList()
{
	$Servers = array("Cornelia", "Kashuan", "Gysahl", "Mysidia", "Istory", "Figaro", 
					 "Wutai", "Trabia", "Lindblum", "Besaid", "Selbina", "Rabanastre", 
					 "Bodhum", "Melmond", "Palamecia", "Saronia", "Fabul", "Karnak");
					 
	return $Servers;
}

# Returns an icon representing the Class Name
function GetClassIcon($ClassName)
{
	$ClassName = trim(str_replace("&nbsp;", "", $ClassName));
	
	$ClassIcons = array("Pugilist" 		=> "images/large/classes/Pugilist.png",
						"Gladiator" 	=> "images/large/classes/Gladiator.png",
						"Marauder" 		=> "images/large/classes/Marauder.png",
						"Archer" 		=> "images/large/classes/Archer.png",
						"Lancer" 		=> "images/large/classes/Lancer.png",
						"Sentinal" 		=> "images/large/classes/Sentinel.png",
						"Sentinel" 		=> "images/large/classes/Sentinel.png",
						"Thaumaturge"	=> "images/large/classes/Thaumaturge.png",
						"Conjurer" 		=> "images/large/classes/Conjurer.png",
						"Carpenter" 	=> "images/large/classes/Carpenter.png",
						"Blacksmith" 	=> "images/large/classes/Blacksmith.png",
						"Armorer" 		=> "images/large/classes/Armorer.png",
						"Goldsmith" 	=> "images/large/classes/Goldsmith.png",
						"Leatherworker"	=> "images/large/classes/Tanner.png",
						"Tanner"		=> "images/large/classes/Tanner.png",
						"Weaver" 		=> "images/large/classes/Weaver.png",
						"Alchemist" 	=> "images/large/classes/Alchemist.png",
						"Culinarian" 	=> "images/large/classes/Culinarian.png",
						"Miner" 		=> "images/large/classes/Miner.png",
						"Botanist" 		=> "images/large/classes/Botanist.png",
						"Fisher" 		=> "images/large/classes/Fisher.png");
	
	$icon = $ClassIcons[$ClassName];				
	return $icon;
}

# Returns correct Rank Icon
function GetRankIcon($Num)
{
	$icon = NULL;
	if ($Num == 1)
		$icon = '<img src="images/icons/medals/1.png" />';
	if ($Num == 2)
		$icon = '<img src="images/icons/medals/2.png" />';
	if ($Num == 3)
		$icon = '<img src="images/icons/medals/3.png" />';
	if ($Num >= 4 && $Num <= 10)
		$icon = '<img src="images/icons/medals/4-10.png" />';
	if ($Num >= 11 && $Num <= 30)
		$icon = '<img src="images/icons/medals/11-30.png" />';
	if ($Num >= 31 && $Num <= 50)
		$icon = '<img src="images/icons/medals/31-50.png" />';
	if ($Num >= 51 && $Num <= 70)
		$icon = '<img src="images/icons/medals/51-70.png" />';
	if ($Num >= 71 && $Num <= 100)
		$icon = '<img src="images/icons/medals/71-100.png" />';
		
	return $icon;
}

# Function to check if clean name exists already (REQUIRES DATABASE CONNECTION PASSED THROUGH)
function CheckCleanName($DB, $name)
{
	$Results = $DB->GetData(TBL_CHARACTERS, TBL_CHARACTERS_CLEANNAME, true, TBL_CHARACTERS_CLEANNAME ."='". $name ."'");
	
	if ($Results[0] > 1)
		return false;
	else
		return true;
}

# Gets the EXP Per Leve
function GetEXPPerLevel()
{
	return array("0", "570", "1270", "2150", "3250", "4750", "6550", "8850", "12050", "16350", "21350", "27250", "34050", "41750", "50450",
				 "60150", "71150", "83150", "93150", "111150", "127150", "147150", "169150", "192150", "217150", "244150", "273150", "304150",
				 "337150", "372150", "410150", "455150", "502150", "552150", "605150", "661150", "720150", "782150", "847150", "915150", "986150",
				 "1060150", "1138150", "1219150", "1304150", "1393150", "1486150", "1582150", "1682150", "1782150");	
}

# Returns the correct index based on class name
function GetIndex($ClassName)
{
	switch($ClassName)
	{
		// Dow
		case 'Gladiator': 	return 1;	
		case 'Pugilist': 	return 0;	
		case 'Marauder': 	return 2;	
		case 'Lancer': 		return 4;	
		case 'Archer': 		return 3;	
		case 'Sentinel': 	return 5;	
		
		// Dom
		case 'Conjurer': 	return 7;	
		case 'Thaumaturge': return 6;	
		
		// Dol
		case 'Carpenter': 	return 8;	
		case 'Blacksmith': 	return 9;	
		case 'Armorer': 	return 10;	
		case 'Goldsmith': 	return 11;	
		case 'Tanner': 		return 12;	
		case 'Weaver': 		return 13;	
		case 'Alchemist': 	return 14;	
		case 'Culinarian': 	return 15;	
		
		case 'Miner': 		return 16;	
		case 'Botanist': 	return 17;	
		case 'Fisher': 		return 18;	
	}
}



function getClassArray(){
	return array(
			'0' => "Misc"
			,'2' => "Pugilist"
			,'3' => "Gladiator"
			,'4' => "Marauder"
			,'7' => "Archer"
			,'8' => "Lancer"
			,'10' => "Sentinel"
			,'22' => "Thaumaturge"
			,'23' => "Conjurer"
			,'29' => "Carpenter"
			,'30' => "Blacksmith"
			,'31' => "Armorer"
			,'32' => "Goldsmith"
			,'33' => "Tanner"
			,'34' => "Weaver"
			,'35' => "Alchemist"
			,'36' => "Culinarian"
			,'39' => "Miner"
			,'40' => "Botanist"
			,'41' => "Fisher"
			);
}
# Returns class based on a number
function GetClass($num)
{
	$jobs = getClassArray();
	
	if(array_key_exists($num, $jobs))
	{
		return $jobs[$num];
	}else
	{
		return "???";
	}
	
}
# Returns class based on a number
function GetClassByName($name)
{
	$jobs = getClassArray();
	
	foreach($jobs as $key => $value)
	{
		if($value == $name)
		{
			return $key;
		}

	}
	return "???";	
}

#-----------------------------------------------------------------------------------
# Parse a string for @ tags
function ParseStringTags($string)
{
	// Set Tag
	$Tag = "@";
	
	// Split String
	$string_array = explode(" ", $string);
	
	// Go through seeing any strings have the @ sign at the start, add it to our arrayz!
	$temp_string_array = array();
	$i = 0;
	foreach($string_array as $word)
	{
		$word = str_replace(".", "", $word);
		$word = str_replace(",", "", $word);
		$word = str_replace("!", "", $word);
		$word = str_replace("?", "", $word);
		$word = str_replace(":", "", $word);
		$word = str_replace(";", "", $word);
		$word = str_replace("-", "", $word);
		$word = str_replace("-", "", $word);
		$word = str_replace("#", "", $word);
		$word = str_replace("(", "", $word);
		$word = str_replace(")", "", $word);
		
		// Character first names have a minimum of 3 letters
		if ($word[0] == "@" && strlen($word) > 3)
		{
			$temp_string_array[$i] = $word;	
		}
		$i++;
	}
	
	// Now check the array has values!
	if (count($temp_string_array) > 0)
	{
		return $temp_string_array;
	}
}

# Method to fix chat stuff
function ChatFix($string)
{
	$string = str_replace("\\\\\\", "", $string);
	
	return $string;
}

# Method to reduce character names down to lowercase/nospace, Created by Ushio
function CleanName($string) 
{
	// trim spaces
	$string = str_replace(" ", "", $string);
	// to lower case
	$string = strtolower($string);
	// return string
	return $string;
} 

# Function to detectu url queries 
function DetectQuery()
{
	// Gets the URL
	$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
	
	// parse the url
	$url_array = parse_url($url);
	//print_r($url_array);
	
	// Get the query
	$query = $url_array['query'];
	$query_array = explode("&", $query);
	$option = $query_array[0];
	unset($query_array[0]);
	//Show($query_array);
	
	// Is the data numeric? If so, its ID, else try find name
	if (stripos($option, "-") !== false)
		return array(NULL, $option, $query_array);
	else if(is_numeric($option))
		return array(TBL_CHARACTERS_CICUID, $option, $query_array);
	else
		return array(TBL_CHARACTERS_CLEANNAME, $option, $query_array);
}
	
# Method to get current url (for call back use)
function PageURL()
{
	$pageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") 
		$pageURL .= "s";
		
	$pageURL .= "://";
	
	if ($_SERVER["SERVER_PORT"] != "80") 
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	else
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	return $pageURL;
}

# Set Page Title
function SetPageTitle()
{
	$id = $_GET['id'];
	$name = $_GET['name'];
	$linkshell = $_GET['gcid'];
	
	if (empty($linkshell))
	{
		if (isset($id))
		{
			$Character = mysql_query("SELECT * FROM ". TBL_CHARACTERS ." WHERE ". TBL_CHARACTERS_CICUID ."='". $id ."'");  
			if (!$Character) { echo mysql_error(); }
			
			// Set Data Array
			$data = mysql_fetch_array($Character);
			
			echo $data[TBL_CHARACTERS_NAME] .' XIVPad';
		}
		else if (isset($name))
		{
			echo $name .' XIVPad';
		}
		else
		{
			echo 'FFXIV Lodestone Pads (beta)';
		}
	}
	else
	{
		$ls = mysql_query("SELECT ". TBL_LINKSHELL_NAME ." FROM ". TBL_LINKSHELL ." WHERE ". TBL_LINKSHELL_ID ."='". $linkshell ."'"); 
		if (!$ls) { echo mysql_error(); }
		
		// Set Data Array
		$title = mysql_fetch_array($ls);
		
		// Get Data from table field			
		echo $title[TBL_LINKSHELL_NAME] .' XIVPad';
	}
}

# Set User Has Character
function UserHasCharacter($session_username)
{
	
	if (isset($session_username))
	{
		$sql = mysql_query("SELECT * FROM ". TBL_USERS ." WHERE ". TBL_USERS_USERNAME ."='". $session_username ."'");  
		if (!$sql) { echo mysql_error(); }
		
		// Set Data Array
		$data = mysql_fetch_array($sql);
		
		if (!empty($data[TBL_USERS_CHARACTER]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

# Method to translate time into formal
function TranslateTime($time, $long = false, $opt = NULL)
{
	$TimeNow = time();
	$TimeThen = $time;
	
	$Difference = $TimeNow - $TimeThen;
	
	# Compare Differences in time for the different outputs 
	#-----------------------------------------------------------------------------------------				Instant
	if ($Difference == 0)
		return '<span style="color:#5FA400;font-weight:bold;">Now!</span>';
	#-----------------------------------------------------------------------------------------				Seconds
	else if ($Difference < 60)
		return '<span style="color:#090;font-weight:bold;">'. $Difference .' seconds ago.</span>';
	#-----------------------------------------------------------------------------------------				Minutes
	else if ($Difference < 3600)
	{
		$Minutes = round($Difference / 60);
		return '<span style="color:#33A4FF;font-weight:bold;">'. $Minutes .' minutes ago.</span>';
	}
	#-----------------------------------------------------------------------------------------				Hours (below 3)
	else if ($Difference < 10800)
	{
		$Hours = floor($Difference / 3600);
		$Seconds = $Difference % 3600;
		$Minutes = floor($Seconds / 60);
		return '<span style="font-weight:bold;">'. $Hours ."hrs ". $Minutes ."mins ago.</span>";
	}
	#-----------------------------------------------------------------------------------------				Hours (above 3)
	else if ($Difference < 86400)
	{
		$Hours = floor($Difference / 3600);
		$Seconds = $Difference % 3600;
		$Minutes = round($Seconds / 60);
		return $Hours ."hrs ". $Minutes ."mins ago.";
	}
	#-----------------------------------------------------------------------------------------				Yesterday
	else if ($Difference < (86400*2))
		return "<em>Yesterday</em>";
	#-----------------------------------------------------------------------------------------				General date/time
	else
	{
		if ($long)
			return date('M d Y, g:i a', $TimeThen);
		else
			return date('M d Y', $TimeThen);
	}
	#-----------------------------------------------------------------------------------------	
}

# Method to translate to unix
function DateToUnix($Date)
{
	$Data = explode("/", $Date);
	if (count($Data) == 3)
		return mktime(0, 0, 0, $Data[0], $Data[1], $Data[2]);
	else
		return false;
}

# Prints out variables with pre format and using print_r()
function Show($Variable)
{
	echo '<pre>';
	print_r($Variable);
	echo '</pre>';
}

# Sort an array via a key
function sksort(&$array, $subkey, $sort_ascending) 
{
	if (count($array))
		$temp_array[key($array)] = array_shift($array);

	foreach($array as $key => $val){
		$offset = 0;
		$found = false;
		foreach($temp_array as $tmp_key => $tmp_val)
		{
			if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
			{
				$temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
											array($key => $val),
											array_slice($temp_array,$offset)
										  );
				$found = true;
			}
			$offset++;
		}
		if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
	}

	if ($sort_ascending)
		$array = array_reverse($temp_array);
	else 
		$array = $temp_array;
}

# Parse bbcode
function bb_parse($string) 
{ 
	$string = ParseEmotes($string);
	
	while (preg_match_all('`\[(.+?)=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) 
		foreach ($matches[0] as $key => $match) 
		{ 
			list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]); 
			switch ($tag) { 
				case 'b': $replacement = "<strong>$innertext</strong>"; break; 
				case 'i': $replacement = "<em>$innertext</em>"; break; 
				
				case 'size': 
					if (str_ireplace("px", "", $param) > 20)
						$param = 20 .'px';
						
					$replacement = "<span style=\"font-size: $param;\">$innertext</span>"; 
					break; 
					
				case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; break; 
				case 'center': $replacement = "<div class=\"centered\">$innertext</div>"; break; 
				case 'url': $replacement = '<a href="http://' . RemoveHTTP(($param? $param : $innertext)) . "\" target=\"_blank\">$innertext</a>"; break; 
				case 'img': 
				 	$Max_Height = 80;
					$Max_Width = 120;
					
					$size = @getimagesize($innertext);
					$width = $size[0];
					$height = $size[1];
					
					if ($width > $Max_Width)
					{
						$ratio = $Max_Width / $width;
						$new_width = $Max_Width;
						$new_height = $height * $ratio;
					}
					else
						$new_width = $size[0];
						
					if ($height > $Max_Height)
					{
						$ratio = $Max_Height / $height;
						$new_height = $Max_Height;
						$new_width = $width * $ratio;
					}
					else
						$new_height = $size[1];
					
					$replacement = '<a href="'. $innertext .'" target="_blank"><img src="'. $innertext .'" width="'. $new_width .'" height="'. $new_height .'" /></a>'; 
					break; 
				break; 
			} 
			$string = str_replace($match, $replacement, $string); 
		} 
		
		return $string;
}

# Emotes
function ParseEmotes($string)
{
	$Find = array(':fire:',
				  ':ice:',
				  ':earth:',
				  ':thunder:',
				  ':water:',
				  ':wind:',
				  
				  ':like:',
				  
				  ':lsleader:',
				  ':lsmaster:',
				  ':lsmember:',
				  
				  ':tleft:',
				  ':tright:',
				  
				  ':auction:',
				  ':editor:',
				  ':cookie:',
				  ':atecookie:',
				  
				  ':smile:',
				  ':cool:',
				  ':grin:',
				  ':zzz:',
				  ':evil:',
				  ':lol:',
				  ':sad:',
				  
				  ':pv:',
				  
				  ':eek:',
				  ':cry:',
				  ':shy:',
				  ':yell:',
				  ':wink:',
				  ':surprise:',
				  ':green:',
				  ':chaching:',
				  ':confused:',
				  ':kitty:',
				  ':angry:',
				  ':no:',
				  ':snowman:',
				  
				  ':star:',
				  ':cake:',
				  ':coffee:');
				  
	$Replace = array('<img src="images/icons/elementals/elemental_fire.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/elementals/elemental_ice.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/elementals/elemental_stone.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/elementals/elemental_thunder.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/elementals/elemental_water.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/elementals/elemental_wind.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/comments/like.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/ffspecific/ls_leader.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/ffspecific/ls_master.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/ffspecific/ls_normal.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/ffspecific/translate_left.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/ffspecific/translate_rght.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/smilies/auction.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/editor.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/misc/pads.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/cookie-bite.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/smilies/smiley.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-cool.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-grin.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-sleep.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-twist.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-lol.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-sad.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/pv.png" style="vertical-align:bottom;" />',
					
					 '<img src="images/icons/smilies/smiley-eek.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-cry.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-red.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-yell.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-wink.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-surprise.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-mr-green.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-money.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-confuse.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-kitty.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-mad.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/smiley-neutral.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/snowman-hat.png" style="vertical-align:bottom;" />',
					 
					 '<img src="images/icons/misc/star.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/cake.png" style="vertical-align:bottom;" />',
					 '<img src="images/icons/smilies/cup.png" style="vertical-align:bottom;" />');

	$string = str_ireplace($Find, $Replace, $string);

	return $string;	
}

# Small Mugshot
function SmallMug($mug)
{
	$Max_Height = 160;
	$Max_Width = 250;
	
	$size = @getimagesize($mug);
	$width = $size[0];
	$height = $size[1];
	
	if ($width > $Max_Width)
	{
		$ratio = $Max_Width / $width;
		$new_width = $Max_Width;
		$new_height = $height * $ratio;
	}
	else
		$new_width = $size[0];
		
	if ($height > $Max_Height)
	{
		$ratio = $Max_Height / $height;
		$new_height = $Max_Height;
		$new_width = $width * $ratio;
	}
	else
		$new_height = $size[1];
	
	$replacement = '<img src=\''. $mug .'\' width=\''. $new_width .'\' height=\''. $new_height .'\' />'; 
	
	return $replacement;	
}
#-----------------------------------------------------------------------------------
function RemoveHTTP($url='') 
{
	return preg_replace("/^https?:\/\/(.+)$/i","\\1", $url);
}

function ShortenString($input)
{
	if (strlen($input) > 40)
			return "Link";
		else 
			return $input;
}

# Detects links in string
function DetectLinksSimple($string)
{
	$Words = explode(" ", $string);
	$i = 0;
	foreach($Words as $Word)
	{
		if (stripos($Word, "[img]") !== false || stripos($Word, "[url=") !== false)
		{
			$Words[$i] = $Word;
		}
		else
		{
			preg_match('/(http:\/\/[^\s]+)/', $Word, $text);
			$Link = "<a style='text-decoration:underline;color:#4AA500;' target='_blank' href=\"". $text[0] . "\">" . ShortenString($text[0])  . "</a>";
			$NewWord = preg_replace('/(http:\/\/[^\s]+)/', $Link, $Word);
			$Words[$i] = $NewWord;
		}
		$i++;
	}

	return implode(" ", $Words);
}

#-----------------------------------------------------------------------------------
// Gets ip
function GetIP()
{
	if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
	{
		$ip=$_SERVER['HTTP_CLIENT_IP'];
	}
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
	{
		$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	else
	{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	return ip2long($ip);
}

// Get Random colour
function GetColour()
{
	$colour = rand(0,10000000);
	return dechex($colour);
}

#-----------------------------------------------------------------------------------
# Language Functions
function getDefaultLanguage() 
{
   if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
      return parseDefaultLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
   else
      return parseDefaultLanguage(NULL);
}

function parseDefaultLanguage($http_accept, $deflang = "en") 
{
   if(isset($http_accept) && strlen($http_accept) > 1)
   {
      # Split possible languages into array
      $x = explode(",",$http_accept);
      foreach ($x as $val) 
	  {
         #check for q-value and create associative array. No q-value means 1 by rule
         if(preg_match("/(.*);q=([0-1]{0,1}\.\d{0,4})/i",$val,$matches))
            $lang[$matches[1]] = (float)$matches[2];
         else
            $lang[$val] = 1.0;
      }

      #return default language (highest q-value)
      $qval = 0.0;
      foreach ($lang as $key => $value)
	  {
         if ($value > $qval)
		 {
            $qval = (float)$value;
            $deflang = $key;
         }
      }
   }
   return strtolower($deflang);
}

#-----------------------------------------------------------------------------------
# BB Code button hover
function Button_BBCode()
{
	return '<span class="chatbutton with-tooltip" style="cursor:pointer;" title="
			<strong style=\'font-size:11px;\'>BBCode Syntax:</strong><br />
			  <div style=\'padding:5px;line-height:18px;font-size:13px;\'>
				  [b]<span style=\'color:#888;\'>bold text</span>[/b]<br />
				  [i]<span style=\'color:#888;\'>italic text</span>[/i]<br />
				  [size=<span style=\'color:#64AE00;\'>15px</span>]<span style=\'color:#888;\'>some text</span>[/size]<br />
				  [color=<span style=\'color:#64AE00;\'>red</span>]<span style=\'color:#888;\'>some text</span>[/color]<br />
				  [url=<span style=\'color:#64AE00;\'>link</span>]<span style=\'color:#888;\'>some text</span>[/url]<br />
				  [img]<span style=\'color:#888;\'>image url</span>[/img]<br /><br />
				  <span style=\'>color:#888;font-size:11px;font-weight:bold;\'>URLs can also be directly pasted</span>
			  </div>">BBCode</span>';
}

#-----------------------------------------------------------------------------------
# Spam check for contact form
function SpamCheck($message)
{
	$message = strtolower($message);
	
	// Tests
	$test1 = "health insurance";
	$test2 = "nigga";
	$test3 = "penis";
	
	if (stripos($message, $test1) === false &&
		stripos($message, $test2) === false &&
		stripos($message, $test3) === false)
	{
		return true;
	}
	
	return false;	
}

#-----------------------------------------------------------------------------------
# Method to create a 32 character random string
function Random32()
{
	return md5(RandomString(16));
}

# Method to generate a 128 character random string
function Random128()
{
	return md5(RandomString(32)) . md5(RandomString(32)) . md5(RandomString(32)) . md5(RandomString(32));
}

# Generate a random string based ona length.
function RandomString($length)
{
	$randstr = "";
	for($i=0; $i<$length; $i++)
	{
		$randnum = mt_rand(0,61);
		if($randnum < 10)
		{
			$randstr .= chr($randnum+48);
		}
		else if($randnum < 36)
		{
			$randstr .= chr($randnum+55);
		}
		else
		{
			$randstr .= chr($randnum+61);
		}
	}
	return $randstr;
}

#-----------------------------------------------------------------------------------
# Translate with str_replace
function Translate($string, $additional = NULL)
{
	$string = Learn2Spell($string);
	
	# Events
	$string = str_replace("Me > Notorious Monster", EVENT_NMS, $string);
	$string = str_replace("Defeated", EVENT_DEFEATED, $string);
	$string = str_replace("Quests Complete", EVENT_QUESTS, $string);
	$string = str_replace("New Guildskills", EVENT_GUILDSKILLS, $string);
	$string = str_replace("Faction Complete", EVENT_FACTIONS, $string);
	$string = str_replace("ranks. 1 Day", EVENT_RANKS_DAY, $string);
	$string = str_replace("ranks gained", EVENT_RANKS, $string);
	$string = str_replace("Been SPing hardcore", EVENT_SPING, $string);
	$string = str_replace("Ding Rank 50", EVENT_RANK50, $string);
	$string = str_replace("Company Quest Complete", EVENT_COMPANY, $string);
	$string = str_replace("Dungeon Decimated", EVENT_DUNGEON, $string);
	
	# Classes
	$string = str_replace("Leatherworker", XIV_LEATHERWORKER, $string);
	$string = str_replace("Tanner", XIV_LEATHERWORKER, $string);
	
	return $string;
}

# Function to fix spellign mistakes
function Learn2Spell($string)
{
	$string = str_replace("Fraction Elimination", "Faction Complete", $string);
	$string = str_replace("Sentinal", "Sentinel", $string);
	$string = str_replace("Welcome to FFXIV Lodestone Pads", "Welcome to XIVPads", $string);
	$string = str_replace("The Dreamers GospelUldah", "The Dreamers Gospel (Uldah)", $string);
	
	return $string;
}

# Function to fix single quotations on database input
function FixSymbols($String)
{
	$String = utf8_decode($String);
	$String = htmlentities($String);
	$String = stripslashes($String);
	$String = str_replace("'", "&#39;", $String);
	$String = str_replace(">", "&gt;", $String);
	$String = str_replace("<", "&lt;", $String);
	return $String;
}

# This method fixes icon paths
function iconfix($string)
{
	// Fun fun replace.... all ffspecific
	$string = str_replace("icons/nm.gif", 				"images/icons/ffspecific/nms.png", $string);
	$string = str_replace("icons/quest.png", 			"images/icons/ffspecific/quest.png", $string);
	$string = str_replace("icons/guildskill.png", 		"images/icons/ffspecific/guildskill.png", $string);
	$string = str_replace("icons/fraction.png", 		"images/icons/ffspecific/fraction.png", $string);
	$string = str_replace("icons/50ranks.png", 			"images/icons/ffspecific/50ranks.png", $string);
	$string = str_replace("icons/leveled.png", 			"images/icons/ffspecific/icon_notice.png", $string);
	$string = str_replace("icons/levelled.png", 		"images/icons/ffspecific/icon_notice.png", $string);
	$string = str_replace("icons/icon_notice.png", 		"images/icons/ffspecific/icon_notice.png", $string);
	$string = str_replace("icons/translate_left.png", 	"images/icons/ffspecific/translate_left.png", $string);
	$string = str_replace("icons/translate_right.png", 	"images/icons/ffspecific/translate_right.png", $string);
	$string = str_replace("icons/exp.png", 				"images/icons/ffspecific/exp.png", $string);
	$string = str_replace("icons/translate_rght.png", 	"images/icons/ffspecific/translate_rght.png", $string);
	$string = str_replace("icons/translate_left.png", 	"images/icons/ffspecific/translate_left.png", $string);
	$string = str_replace("icons/retired.png", 			"images/icons/ffspecific/retired.png", $string);
	$string = str_replace("icons/star.png", 			"images/icons/misc/star.png", $string);
	$string = str_replace("icons/dungeon.gif", 			"images/icons/ffspecific/dungeon.png", $string);
	$string = str_replace("icons/company.png", 			"images/icons/ffspecific/company.png", $string);
	$string = str_replace("icons/exchanges.png", 		"images/icons/ffspecific/exchanges.png", $string);
	
	// Return
	return $string;
}

# Fix existing URLs (Incase bookmarked)
function FixURL($Page)
{
	$Page = str_replace('i=linkshells', '-Linkshells', $Page);	
	$Page = str_replace('i=characters', '-Characters', $Page);	
	
	return $Page;
}

# Display page is missing
function Missing()
{
	echo '<div class="notice">
		<strong>System Error: Page not found</strong> 
		<br />
		The page you are looking for could not be found, it could be getting updated. Please try refreshing.
	</div>';
}
?>
